import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return bool布尔型
     */
    public boolean isCompleteTree (TreeNode root) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        list.add(root.val);
        while(!q.isEmpty()) {
            int size = q.size();
            for(int i = 0;i<size;i++) {
                TreeNode cur = q.poll();
                if(cur.left == null) {
                    list.add(0);
                } else {
                    q.offer(cur.left);
                    list.add(cur.left.val);
                }
                if(cur.right == null) {
                    list.add(0);
                } else {
                    q.offer(cur.right);
                    list.add(cur.right.val);
                }
            }
        }
        for(int i = 0;i<list.size();i++) {
            if(list.get(i) == 0) {
                while(i < list.size()) {
                    if(list.get(i++) != 0 ) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}